from exts import db
from datetime import datetime


class BoardModel(db.Model):
    __tablename__ = 'board'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True,comment='板块id')
    name = db.Column(db.String(20), nullable=False,comment='板块名称')
    create_time = db.Column(db.DateTime, default=datetime.now,comment='板块创建时间')
    is_active = db.Column(db.Boolean, default=True,comment='板块状态')


class PostModel(db.Model):
    __tablename__ = 'post'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True,comment='帖子id')
    title = db.Column(db.String(50), nullable=False,comment='帖子标题')
    content = db.Column(db.Text, nullable=False,comment='帖子内容')
    create_time = db.Column(db.DateTime, default=datetime.now,comment='帖子创建时间')
    read_count = db.Column(db.Integer, default=0,comment='帖子阅读数')
    is_active = db.Column(db.Boolean, default=True,comment='帖子状态')
    board_id = db.Column(db.Integer, db.ForeignKey('board.id'),comment='板块id')
    author_id = db.Column(db.String(100), db.ForeignKey('user.id'),nullable=False,comment='作者id')

    board = db.relationship('BoardModel', backref='posts')
    author = db.relationship('UserModel', backref='posts')


class CommentModel(db.Model):
    __tablename__ = 'comment'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True,comment='评论id')
    content = db.Column(db.Text, nullable=False,comment='评论内容')
    create_time = db.Column(db.DateTime, default=datetime.now,comment='评论创建时间')
    is_active = db.Column(db.Boolean, default=True,comment='评论状态')
    post_id = db.Column(db.Integer, db.ForeignKey('post.id'))
    author_id = db.Column(db.String(100), db.ForeignKey('user.id'),nullable=False,comment='作者id')

    post = db.relationship('PostModel', backref=db.backref('comments', order_by=create_time.desc(),lazy='dynamic'))
    author = db.relationship('UserModel', backref='comments')